package org.darkforest.common.services.impl;

import org.darkforest.common.services.StatProxyService;
import org.darkforest.dao.mapper.ProxyMapper;
import org.darkforest.dao.mapper.StatProxyMapper;
import org.darkforest.model.Proxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by valentina on 15/8/2.
 */
@Service
public class StatProxyServiceImpl implements StatProxyService {

    @Autowired
    private StatProxyMapper statProxyMapper;
    @Autowired
    private ProxyMapper proxyMapper;

    /**
     * 统计表teamcnt字段清零,查询
     * @return
     */
    @Override
    @Transactional
    public List<Proxy> initLastLevelProxy() {
        statProxyMapper.clearTeamCnt();
        List<Proxy> proxyList = proxyMapper.getInitProxyTeamNum();
        int res = statProxyMapper.batchUpdateTeamCnt(proxyList);
        return proxyList;
    }

    /**
     *
     * @param proxy_layer
     * @return
     */
    @Override
    public void updateTeamCnt(Integer proxy_layer) {
//        List<Integer> res = new ArrayList<Integer>();
        //获取这些节点的父节点信息
//        List<Proxy> parents = statProxyMapper.getParentProxyTeamCnt(proxy_layer);
//        if(parents!=null && parents.size()>0){
//            for (Proxy proxy : parents){
//                res.add(proxy.getParentProxy());
//            }
//            statProxyMapper.batchUpdateTeamCnt(parents);
//        }
//        return res;
        /**
         * 获取每级别团队数量不为0的节点，分组统计父节点团队需增加人数
         */
        List<Proxy> ups = statProxyMapper.getParentProxyTeamCnt(proxy_layer);
        statProxyMapper.batchUpdateTeamCnt(ups);
    }

    @Override
    public List<Integer> getLeafParent() {
        return statProxyMapper.getLeafParent();
    }

    @Override
    public int getProxyMaxLayer() {
        return proxyMapper.getMaxLayer();
    }
}
